package com.goods.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.goods.business.mapper.*;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.*;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockItemVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.service.OutStockService;
import com.goods.system.mapper.UserMapper;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired
    OutStockMapper outStockMapper;

    @Autowired
    ConsumerMapper consumerMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    OutStockInfoMapper outStockInfoMapper;

    @Autowired
    ProductStockMapper productStockMapper;

    @Autowired
    UserService userService;

    @Override
    public List<OutStock> findOutStockList(Integer status) {
        OutStock outStock = new OutStock();
        outStock.setStatus(status);
        List<OutStock> outStockList = outStockMapper.select(outStock);
        return outStockList;
    }

    @Override
    public Consumer findConsumer(Long consumerId) {
        Consumer consumer = new Consumer();
        consumer.setId(consumerId);
        return consumerMapper.selectOne(consumer);
    }

    @Override
    public List<OutStockItemVO> detail(Long id, Integer pageNum) {
        List<OutStockItemVO> outStockItemVOList = outStockMapper.selectOutStockDetailVo(id);
        return outStockItemVOList;
    }

    @Override
    public Consumer findConsumerId(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectOne(outStock);
        Consumer consumer = new Consumer();
        consumer.setId(outStock1.getConsumerId());
        return consumerMapper.selectOne(consumer);
    }

    @Override
    public List<OutStock> findOutStockList(Integer status, String outNum, String type) {
        List<OutStock> outStockList = outStockMapper.selectLike(status,outNum,type);
        return outStockList;
    }

    @Override
    public int remove(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectOne(outStock);
        outStock1.setStatus(1);
        return outStockMapper.updateByPrimaryKey(outStock1);
    }

    @Override
    public int back(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectOne(outStock);
        outStock1.setStatus(0);
        return outStockMapper.updateByPrimaryKey(outStock1);
    }

    @Override
    public int delete(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        return outStockMapper.delete(outStock);
    }

    @Override
    public int addOutStock(OutStockVO outStockVO) {
        OutStock outStock = new OutStock();

        //    private String outNum;
        outStock.setOutNum(UUID.randomUUID().toString().replaceAll("-",""));
        //    private Integer type;
        outStock.setType(outStockVO.getType());
        //    private String operator;
        try {
            UserInfoVO userInfoVO = userService.info();
            outStock.setOperator(userInfoVO.getUsername());
        } catch (SystemException e) {
            e.printStackTrace();
        }
        //    private Date createTime;
        outStock.setCreateTime(new Date());
        //    private Integer productNumber;
        Integer i = 0;
        List<Object> products = outStockVO.getProducts();
        for (Object product : products) {
            HashMap hashMap = JSONObject.parseObject(JSONObject.toJSONString(product), HashMap.class);
            Integer productNumber = (Integer)hashMap.get("productNumber");
            i += productNumber;
            Product product1 = new Product();
            product1.setId(Long.parseLong(hashMap.get("productId").toString()));
            Product product2 = productMapper.selectOne(product1);
            String pNum = product2.getPNum();

            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setOutNum(outStock.getOutNum());
            outStockInfo.setPNum(pNum);
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfo.setProductNumber(productNumber);
            outStockInfoMapper.insert(outStockInfo);
        }

        outStock.setProductNumber(i);
        //    private Long consumerId;
        if (StringUtils.isEmpty(outStockVO.getConsumerId())){
            Consumer consumer = new Consumer();
            consumer.setName(outStockVO.getName());
            consumer.setAddress(outStockVO.getAddress());
            consumer.setPhone(outStockVO.getPhone());
            consumer.setContact(outStockVO.getContact());
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumerMapper.insert(consumer);
            outStock.setConsumerId(consumer.getId());
        }else {
            outStock.setConsumerId(outStockVO.getId());
        }
        //    private String remark;
        outStock.setRemark(outStockVO.getRemark());
        //    private Integer status;
        outStock.setStatus(2);
        //    private Integer priority;
        outStock.setPriority(outStockVO.getPriority());
        return outStockMapper.insert(outStock);
    }

    @Override
    public int publish(long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectOne(outStock);

        OutStockInfo outStockInfo = new OutStockInfo();
        outStockInfo.setOutNum(outStock1.getOutNum());
        List<OutStockInfo> outStockInfo1 = outStockInfoMapper.select(outStockInfo);
        for (OutStockInfo stockInfo : outStockInfo1) {
            Integer productNumber = stockInfo.getProductNumber();
            ProductStock productStock = new ProductStock();
            productStock.setPNum(stockInfo.getPNum());
            ProductStock productStock1 = productStockMapper.selectOne(productStock);
            productStock1.setStock(productStock1.getStock() - productNumber);
            productStockMapper.updateByPrimaryKey(productStock1);
        }

        outStock1.setStatus(0);
        return outStockMapper.updateByPrimaryKey(outStock1);
    }

}
